<?php
namespace app\admin\controller;
use cmf\controller\AdminBaseController;
use think\Db;

class WebmailController extends AdminBaseController
{
	public function index()
	{
		$content = hook_one('admin_webmail_index_view');
		if (!empty($content)) {
			return $content;
		}

		$where = ['id' => ['neq', 0]];
		/**搜索条件**/
		$websitename = $this->request->param('websitename');
		if ($websitename) {
			$where['websitename'] = ['like', "%$websitename%"];
		}

		$webmailList = Db::name('webmail')
			->where($where)
			->order("id DESC")
			->paginate(50);

		$webmailList->appends(['websitename' => $websitename]);
		// 获取分页显示
		$page = $webmailList->render();

		$this->assign("page", $page);
		$this->assign("webmailList", $webmailList);
		return $this->fetch();
	}

	public function add()
	{
		return $this->fetch();
	}

	public function addPost()
	{
		if ($this->request->isPost()) {
			$webArr = explode('.', $_POST['websitename']);
			//检测唯一性
			$websitename = trim($_POST['websitename']);
			$webmailList = Db::name('webmail')->where("websitename='$websitename'")->find();
			if($webmailList){
				$this->error("当前站点已存在");
			}

			if (!empty($_POST['websitename']) && count($webArr) == 3) {
				$_POST["create_time"] = date("Y-m-d H:i:s");
				$_POST["update_time"] = date("Y-m-d H:i:s");
				$_POST["websitename"] = trim($_POST["websitename"]);
				$_POST["emailname"] = trim($_POST["emailname"]);
				$_POST["emailpawd"]   = trim($_POST["emailpawd"]);

				$result	= DB::name('webmail')->insertGetId($_POST);
				if ($result !== false) {
						$this->success("添加成功！", url("webmail/index"));
					} else {
						$this->error("添加失败！");
					}
			} else {
				$this->error("站点名称能为空或者格式不正确");
			}
		}
	}

	public function edit()
	{
		$content = hook_one('admin_webmail_edit_view');

		if (!empty($content)) {
			return $content;
		}

		$id      = $this->request->param('id', 0, 'intval');
		$webmail = DB::name('webmail')->where(["id" => $id])->find();
		$this->assign($webmail);
		return $this->fetch();
	}

	public function editPost()
	{
		if ($this->request->isPost()) {
			// $webArr = explode('.', $_POST['websitename']);
			//检测唯一性
			// $websitename = trim($_POST['websitename']);
			// $webmailList = Db::name('webmail')->where("websitename='$websitename'")->find();
			// // echo Db::name('webmail')->getLastSql();
			// if($webmailList){
			// 	$this->error("当前站点已存在");
			// }

			if (!empty($_POST['id']) || !empty($_POST['emailname']) || !empty($_POST['emailpawd'])) {
				$_POST["update_time"] = date("Y-m-d H:i:s");
				$_POST["emailname"] = trim($_POST["emailname"]);
				$_POST["emailpawd"]   = trim($_POST["emailpawd"]);

				$result = DB::name('webmail')->update($_POST);
				if ($result !== false) {
						$this->success("添加成功！", url("webmail/index"));
					} else {
						$this->error("添加失败！");
					}
			} else {
				$this->error("站点邮件和者密码不能为空");
			}
		}
	}

	public function delete()
	{
		$id = $this->request->param('id', 0, 'intval');

		if (Db::name('webmail')->delete($id) !== false) {
			$this->success("删除成功！");
		} else {
			$this->error("删除失败！");
		}
	}
}